package com.office.personalOffice.nameCard.personalNameCard.service.impl;

import com.office.personalFolders.myFolder.pojo.TbStaff;
import com.office.personalOffice.nameCard.nameCardList.dao.IsNameCardListDao;
import com.office.personalOffice.nameCard.nameCardList.pojo.OaNamecard;
import com.office.personalOffice.nameCard.nameCardList.pojo.OaNamecardlist;
import com.office.personalOffice.nameCard.nameCardList.service.IsNameCardListService;
import com.office.personalOffice.nameCard.personalNameCard.dao.IsPNCDao;
import com.office.personalOffice.nameCard.personalNameCard.pojo.TbPosition;
import com.office.personalOffice.nameCard.personalNameCard.service.IsPNCService;
import com.office.personalOffice.personalInfo.dao.IPersonalInfo;
import com.office.personalOffice.personalInfoQuery.model.TbDept;
import com.office.personalOffice.personalInfoQuery.service.IPersonalInfoQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @project_name: OA
 * @description:
 * @author: LaoWang
 * @date: 2022/11/1 18:32
 */

@Service
public class PNCService implements IsPNCService {

    @Autowired
    private IsPNCDao isPNCDao;

    @Autowired
    @Lazy
    private IsNameCardListService isNameCardListService;


    @Autowired
    private IsNameCardListDao isNameCardListDao;


    @Autowired
    private IPersonalInfo iPersonalInfo;


    @Autowired
    private IPersonalInfoQueryService iPersonalInfoQueryService;

    public IsPNCDao getIsPNCDao() {
        return isPNCDao;
    }
    public void setIsPNCDao(IsPNCDao isPNCDao) {
        this.isPNCDao = isPNCDao;
    }



    /**
     * 根据名片夹分类名查询用户所有名片夹（名片夹分类模块调用）
     * @param oaNamecardlist 查询条件
     * @return 名片夹信息集合
     */
    public List<OaNamecard>  getAllNamecardByName(OaNamecardlist oaNamecardlist){
        return isPNCDao.getAllNamecardByName(oaNamecardlist);
    }


    /**
     * 查询当前用户通讯录所有的名片
     * @param maillistId 查询条件
     * @return 名片夹信息集合
     */
    public List<OaNamecard>  getAllNamecardByMaillistId(String maillistId){
        return isPNCDao.getAllNamecardByMaillistId(maillistId);
    }



    /**
     * 获取所有名片夹分类
     * @param
     * @return 名片夹类信息集合
     */
    public List<OaNamecardlist>  getAllNamecard(){
        OaNamecardlist oaNamecardlist=new OaNamecardlist();
        oaNamecardlist.setNamecardlistUsable(1);
        return isNameCardListService.getOaNamecardlistByOaNamecardlist(oaNamecardlist);
    }




    /**
     * 添加名片验证（每个用户的所拥有的每个名片只能存放在一个名片夹类中）
     * @param
     * @return 名片夹类信息集合
     */
    public String NameCardYZ(OaNamecardlist oaNamecardlist,HttpSession session){
        oaNamecardlist.setNamecardlistName("我的名片夹");
        com.office.personalOffice.personalInfo.model.TbStaff staff1 = (com.office.personalOffice.personalInfo.model.TbStaff) session.getAttribute("loginUser");
        oaNamecardlist.setNamecardlistMaillistId(staff1.getId().toString());
        oaNamecardlist.setNamecardlistUsable(1);
        oaNamecardlist.setNamecardlistNamecardId(staff1.getId().toString());
        List<OaNamecardlist> o = isNameCardListService.getOaNamecardlistByOaNamecardlist(oaNamecardlist);
        if(o!=null&&o.size()>0){
            return "true";
        }else {
            return "false";
        }
    }




    /**
     * 添加名片
     * @param oaNamecardlist 添加条件
     * @return
     */
    public String addOaNamecardlist(OaNamecardlist oaNamecardlist,HttpSession session){
        //名片夹类添加记录
        oaNamecardlist.setNamecardlistName("我的名片夹");
        com.office.personalOffice.personalInfo.model.TbStaff staff1 = (com.office.personalOffice.personalInfo.model.TbStaff) session.getAttribute("loginUser");
        oaNamecardlist.setNamecardlistMaillistId(staff1.getId().toString());
        oaNamecardlist.setNamecardlistUsable(1);
        oaNamecardlist.setNamecardlistCreatetime(new Date());
        oaNamecardlist.setNamecardlistNamecardId(staff1.getId().toString());
        Integer integer = isPNCDao.addOaNamecardlist(oaNamecardlist);

        //名片表添加自己的名片记录
        OaNamecard oaNamecard=new OaNamecard();
        oaNamecard.setNamecardEmpId(staff1.getId().toString());
        Integer integer1 = isPNCDao.addOaNamecard(oaNamecard);

        if(integer==1 && integer1==1){
            return "true";
        }else {
            return "false";
        }


    }


    /**
     * 删除名片
     * @param oaNamecard 添加条件
     * @return
     */
    public Integer deleteNameCard(OaNamecard oaNamecard){
        return isPNCDao.deleteNameCard(oaNamecard);
    }



    /**
     * 查询名片信息即查询员工信息
     * @param tbStaff 查询条件
     * @return
     */
       public com.office.personalOffice.personalInfo.model.TbStaff queryNameCard(com.office.personalOffice.personalInfo.model.TbStaff   tbStaff){
            return iPersonalInfo.getStaffByCondition(tbStaff);
        }



    /**
     * 修改名片信息即修改员工信息
     * @param tbStaff 修改条件
     * @return
     */
    public String updateNameCard(com.office.personalOffice.personalInfo.model.TbStaff   tbStaff){
        Integer integer = iPersonalInfo.updateStaffInfo(tbStaff);
        if(integer==1){
            return "true";
        }else {
            return "false";
        }
    }


    /**
     * 拿取所有的部门信息
     * @return
     */
    public List<TbDept> getAllDept() {
        return iPersonalInfoQueryService.getAllDept();
    }





    /**
     * 获取所有职位
     * @return
     */
    public List<TbPosition>  getAllTbPosition(TbPosition tbPosition){
        return isPNCDao.getAllTbPosition(tbPosition);
    }




    /**
     * 移动名片到某个名片夹分类中验证是否有该名片
     * @return
     */
    public String nameCardListYZ(OaNamecardlist oaNamecardlist,HttpSession session){
        com.office.personalOffice.personalInfo.model.TbStaff staff1 = (com.office.personalOffice.personalInfo.model.TbStaff) session.getAttribute("loginUser");
        oaNamecardlist.setNamecardlistMaillistId(staff1.getId().toString());
        oaNamecardlist.setNamecardlistUsable(1);
        List<OaNamecardlist> o = isNameCardListService.getOaNamecardlistByOaNamecardlist(oaNamecardlist);
        if(o!=null && o.size()>0){
            return "true";
        }else {
            return "false";
        }

    }




    /**
     * 移动名片
     * @return
     */
    public String nameCardListRemove(@RequestBody OaNamecardlist oaNamecardlist,HttpSession session){

        com.office.personalOffice.personalInfo.model.TbStaff staff1 = (com.office.personalOffice.personalInfo.model.TbStaff) session.getAttribute("loginUser");
        //修改之前该名片所在分类的记录的状态
        OaNamecardlist oaNamecardlist1=new OaNamecardlist();

        //先获取之前所在分类的分类名
        OaNamecardlist o=new OaNamecardlist();
        o.setNamecardlistMaillistId(staff1.getId().toString());
        o.setNamecardlistUsable(1);
        o.setNamecardlistNamecardId(oaNamecardlist.getNamecardlistNamecardId());
        OaNamecardlist oaNamecardlist2 = isNameCardListService.getOaNamecardlistByOaNamecardlist(o).get(0);
        oaNamecardlist1.setNamecardlistName(oaNamecardlist2.getNamecardlistName());

        oaNamecardlist1.setNamecardlistMaillistId(staff1.getId().toString());
        oaNamecardlist1.setNamecardlistNamecardId(oaNamecardlist.getNamecardlistNamecardId());
        oaNamecardlist1.setNamecardlistDeletetime(new Date());
        Integer integer1 = isNameCardListDao.deleteOaNamecardlist(oaNamecardlist1);


        //增加一条新分类的该名片记录
        oaNamecardlist.setNamecardlistMaillistId(staff1.getId().toString());
        oaNamecardlist.setNamecardlistUsable(1);
        oaNamecardlist.setNamecardlistCreatetime(new Date());
        Integer integer = isPNCDao.addOaNamecardlist(oaNamecardlist);



        if(integer==1 && integer1==1){
            return "true";
        }else {
            return "false";
        }
    }


}
